import _ from 'lodash'

export default {
    clickoutside: {
        bind(element, binding) {
            function documentHandler(event_) {
                if (element.contains(event_.target)) {
                    return false
                }
                if (binding.expression || _.isFunction(binding.value)) {
                    binding.value(event_)
                }
                return true
            }
            element.__vueClickOutside__ = documentHandler
            document.addEventListener('click', documentHandler)
        },
        unbind(element) {
            document.removeEventListener('click', element.__vueClickOutside__)
            delete element.__vueClickOutside__
        },
    },
}
